<!--描述规则-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规则名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="元素编号" prop="elementCode">
        <el-input
          v-model="queryParams.elementCode"
          placeholder="请输入元素编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="元素名称" prop="elementName">
        <el-input
          v-model="queryParams.elementName"
          placeholder="请输入元素名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="值" prop="ruleValue">
        <el-input
          v-model="queryParams.ruleValue"
          placeholder="请输入值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选择" prop="choice">
        <el-input
          v-model="queryParams.choice"
          placeholder="请输入选择"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gyl:rules:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gyl:rules:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gyl:rules:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gyl:rules:export']"
        >导出</el-button>
      </el-col>
      <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="getSelectData"-->
<!--          v-hasPermi="['gyl:rules:export']">获取勾选数据</el-button>-->
<!--      </el-col>-->
      <EasyTable ref="childRef" :tableData="rulesList" :columns="tableColumn" :Shuju="Shuju"  @listen="handleSelectionChange" @changeList="rowsol">
        <vxe-column title="操作" width="200" show-overflow>
          <template #default="{ row }">
            <vxe-button type="text" icon="vxe-icon-edit" @click="handleUpdate(row)">编辑</vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" @click="handleDelete(row)">删除</vxe-button>
          </template>
        </vxe-column>
      </EasyTable>
    </el-row>
    <div style="display: flex; height: 30px; justify-content:flex-end">
      <div class="Size">字体大小</div>
      <el-input class="Einput" v-model="Shuju.fontSize" placeholder="字体大小" clearable @blur="blurs"></el-input>
      <div class="Size">行高</div>
      <el-input class="Einput" v-model="Shuju.Height" placeholder="行高" clearable @blur="blurs">
      </el-input>
      <pagination v-show="total > 0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList" style="width: 800px ; margin-top: -10px"/>
    </div>
<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->
    <!--    修改某一列数据-->
    <el-dialog :visible.sync="opens" width="500px" append-to-body :title="title">
      <el-form label-width="80px">
        <el-form-item label="修改值" prop="category">
          <el-input v-model="list.afterName" placeholder="请输入修改值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitList">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改描述规则维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规则名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入规则名称" />
        </el-form-item>
        <el-form-item label="序号" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="元素编号" prop="elementCode">
          <el-input v-model="form.elementCode" placeholder="请输入元素编号" />
        </el-form-item>
        <el-form-item label="元素名称" prop="elementName">
          <el-input v-model="form.elementName" placeholder="请输入元素名称" />
        </el-form-item>
        <el-form-item label="值" prop="ruleValue">
          <el-input v-model="form.ruleValue" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="选择" prop="choice">
          <el-input v-model="form.choice" placeholder="请输入选择" />
        </el-form-item>
<!--        <el-form-item label="创建时间" prop="createTime">-->
<!--          <el-input v-model="form.createTime" placeholder="请输入元素编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="修改时间" prop="updateTime">-->
<!--          <el-input v-model="form.updateTime" placeholder="请输入元素编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="创建人" prop="createBy">-->
<!--          <el-input v-model="form.createBy" placeholder="请输入元素编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="修改人" prop="updateBy">-->
<!--          <el-input v-model="form.updateBy" placeholder="请输入元素编号" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
        <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRules, getRules, delRules, addRules, updateRules } from "@/api/gyl/rules";
import EasyTable from '@/components/EasyTable/index.vue'
import { getToken } from "../../../utils/auth";
export default {
  name: "Rules",
  components: {
    EasyTable
  },
  data() {
    return {
      opens:false,
      Shuju:{
        //表格字体大小
        fontSize:'12',
        Height:60,
      },
      //修改列表参数
      list:{
        ids:[],
        //点击某一列的字段名称
        itemName:'',
        afterName:'',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 描述规则维护表格数据
      rulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        elementCode: null,
        elementName: null,
        choice: null,
        sort:null,
        ruleValue:null,
        createTime:null,
        updateTime:null,
        createBy:null,
        updateBy:null,
      },
      tableColumn: [
        //{ key: 0, type: 'seq', width: 60, fixed: null },
        { key: 1, type: 'checkbox', width: 50, fixed: null },
        { key: 2, field: 'id', title: 'id', },
        { key: 3, field: 'name', title: '规则名称', },
        { key: 4, field: 'sort', title: '序号', },
        { key: 5, field: 'elementCode', title: '元素编号', },
        { key: 6, field: 'elementName', title: '元素名称', },
        { key: 7, field: 'ruleValue', title: '值', },
        { key: 8, field: 'choice', title: '选择', },
        // { key: 9, field: 'createTime', title: '创建时间', },
        // { key: 10, field: 'updateTime', title: '修改时间', },
        // { key: 11, field: 'createBy', title: '创建人', },
        // { key: 12, field: 'updateBy', title: '修改人', },
      ],
            // 用户导入参数
            upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/gyl/rules/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询描述规则维护列表 */
    getList() {
      this.loading = true;
      listRules(this.queryParams).then(response => {
        this.rulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        elementCode: null,
        elementName: null,
        choice: null,
        createTime: null,
        sort:null,
        ruleValue:null,
        updateTime:null,
        createBy:null,
        updateBy:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加描述规则维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRules(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改描述规则维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除描述规则维护编号为"' + ids + '"的数据项？').then(function() {
        return delRules(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gyl/rules/export', {
        ...this.queryParams
      }, `rules_${new Date().getTime()}.xlsx`)
    },
        /** 导入按钮操作 */
        handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('gyl/rules/importTemplate', {
      }, `rules_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    getSelectData() {
      const childComputedValue = this.$refs.childRef.getSelectData();
      console.log(childComputedValue);
    },
    blurs(){
  if(this.Shuju.fontSize==''||this.Shuju.Height=='')
  {
    this.Shuju.Height=60
    this.Shuju.fontSize = 12
  }
  const Size =  this.$refs.childRef.changeSize();
  console.log(Size)
},
    //获取表单数据
    rowsol(column){
      this.opens=true;
      this.list.itemName = this.getKebabCase(column.column)
      console.log(this.list.itemName)
      this.list.ids= column.List
    },
    //发送修改某一列请求
    submitList(){
      //此处修改接口
      PutMaterialmaintenance(JSON.stringify(this.list)).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.opens = false;
        this.reset();
        this.getList();
      })
    },
    //将驼峰命名改成下划线
    getKebabCase(str) {
      let temp = str.replace(/[A-Z]/g, function(i) {
        return '_' + i.toLowerCase();
      })
      if (temp.slice(0,1) === '_') {
        temp = temp.slice(1);   //如果首字母是大写，执行replace时会多一个_，需要去掉
      }
      return temp;
    },
  }
};
</script>
<style lang="css" scoped>
@import "../CSS/style.css";
::v-deep .el-input__inner {
  height: 29px;
  line-height: 29px;
}
</style>
